﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace MyCollection
{
    public partial class DisplayGoods : Form
    {
        public DisplayGoods()
        {
            InitializeComponent();
        }

        private MyCollectDataSet.CollectProgramRow currentRow;

        public MyCollectDataSet.CollectProgramRow CurrentRow
        {
            get { return currentRow; }
            set { currentRow = value; }
        }

        private MyCollectDataSet.CollectGoodsDataTable tableChange;

        public MyCollectDataSet.CollectGoodsDataTable TableChange
        {
            get { return tableChange; }
            set { tableChange = value; }
        }

        MyCollectDataSet.CollectGoodsDataTable table;
        private void DisplayGoods_Load(object sender, EventArgs e)
        {
            if (currentRow != null)
            {
                table = new MyCollectDataSet.CollectGoodsDataTable();
                MyCollectDataSet.CollectGoodsRow[] rows = currentRow.GetCollectGoodsRows();

                foreach (MyCollectDataSet.CollectGoodsRow row in rows)
                {
                    table.ImportRow(row);
                }
                //绑定table表
                this.dataGridView1.AutoGenerateColumns = false;
                this.dataGridView1.DataSource = table;
            }
            else
            {
                this.dataGridView1.AutoGenerateColumns = false;
                this.dataGridView1.DataSource = tableChange;
            }
            
        }

        /// <summary>
        /// 关闭窗口 检查是否有更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DisplayGoods_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (table == null) return;
            
            MyCollectDataSet.CollectGoodsDataTable dt = table.GetChanges(DataRowState.Modified) as MyCollectDataSet.CollectGoodsDataTable ;
            if ( dt != null)
            {
                
                MyCollectDataSetTableAdapters.CollectGoodsTableAdapter adapter = new MyCollection.MyCollectDataSetTableAdapters.CollectGoodsTableAdapter();
                             
                adapter.Update(dt);
                tableChange = table;
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 4 && e.RowIndex != -1)
            {
                //打开商品所在页 进行购买
                DataGridViewLinkCell link = (DataGridViewLinkCell)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                System.Diagnostics.Process.Start("iexplore.exe", link.Value.ToString());
            }
            else if(e.ColumnIndex == 3 && e.RowIndex != -1)
            {
                //浏览和设置商品详细描述
                MyCollectDataSet.CollectGoodsRow dataRow = (dataGridView1.CurrentRow.DataBoundItem as DataRowView).Row 
                    as MyCollectDataSet.CollectGoodsRow;

                DisplayBody displayForm = new DisplayBody();
                displayForm.Body = dataRow.Body;
                displayForm.SetBody();
                displayForm.ShowDialog();
               
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                table = this.tableChange;
                string filter = "Title like '*" + this.textBox1.Text.Trim() + "*'";

                this.dataGridView1.DataSource = table.Select(filter);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


    }
}